package com.fortify.awb.bugtracker;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * A simple collection class that contains a number of {@link BugtrackerField} objects.  These are what
 * gets submitted to the central repository when filing a bug.
 */
public class BugtrackerFields {

	/**
	 * A collection of the bug tracker fields that will be submitted to the repository for this bug
	 */
	private Map<String, BugtrackerField> fields = new LinkedHashMap<String, BugtrackerField>();

	/**
	 * Returns the collection of bug tracker fields
	 *
	 * @return the collection of bug tracker fields
	 */
	public Map<String, BugtrackerField> getFields() {
		return fields;
	}

	/**
	 * Add a new field with the corresponding name
	 */
	private void addField(String name, BugtrackerField field) {
		fields.put(name, field);
	}

	/**
	 * Create a new field with the specified name and default value, and add it to the collection
	 * @return the newly created field
	 */
	public BugtrackerField addFieldWithDefaultValue(String name, String defaultValue) {
		return addFieldWithDefaultValue(name, name, defaultValue);
	}

	/**
	 * Create a new field with the specified internal name, display name, and default value, and add it to the collection
	 * @return the newly created field
	 */
	public BugtrackerField addFieldWithDefaultValue(String internalName, String displayName, String defaultValue) {
		BugtrackerField field = new BugtrackerField();
		field.setDisplayName(displayName);
		field.setTargetName(internalName);
		field.setDefaultValue(defaultValue);
		field.setValue(defaultValue);
		addField(displayName, field);
		return field;
	}

	/**
	 * Create a new field with the specified name and value, and add it to the collection
	 * @return the newly created field
	 */
	public BugtrackerField addFieldWithValue(String name, String value) {
		BugtrackerField field = new BugtrackerField();
		field.setDisplayName(name);
		field.setTargetName(name);
		field.setValue(value);
		addField(name, field);
		return field;
	}

	/**
	 * Returns the field with the specified display name, or null if it cannot be found
	 *
	 * @return the field with the specified display name, or null if it cannot be found
	 */
	public BugtrackerField getField(String name) {
		Map<String, BugtrackerField> fields = getFields();
		return (fields == null) ? null : fields.get(name); 
	}

	/**
	 * Remove the field with the specified display name
	 */
	public void removeField(String name) {
		fields.remove(name);
	}
}
